<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.6.3/css/layui.css}">
    <link rel="stylesheet" th:href="@{/css/public.css}">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <input type="hidden" name="id" th:value="${clazz.id}" class="layui-input">

    <div class="layui-form-item">
        <label class="layui-form-label">学院名称</label>
        <div class="layui-input-block">
            <select name="collegeName" id="collegeName" lay-filter="collegeName">
                <option th:text="${clazz.collegeName}"  selected> </option>
                <option value=""></option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">专业名称</label>
        <div class="layui-input-block">
            <select name="majorName" id="majorName" lay-filter="majorName">
                <option th:text="${clazz.majorName}"  selected> </option>
                <option value="" ></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">老师</label>
        <div class="layui-input-block">
            <select name="userId" lay-verify="required">
                <option th:each="user : ${users}"
                        th:text="${user.name}" th:value="${user.id}"
                        th:selected="${clazz.userId} eq ${user.id}"></option>
            </select>
        </div>
    </div>



    <div class="layui-form-item">
        <label class="layui-form-label required">班级代号</label>
        <div class="layui-input-block">
            <input type="text" name="clazzCode" th:value="${clazz.clazzCode}" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">班级名称</label>
        <div class="layui-input-block">
            <input type="text" name="clazzName" th:value="${clazz.clazzName}" class="layui-input">
        </div>
    </div>

    <input type="hidden" name="clazzNumber" th:value="${clazz.clazzNumber}" class="layui-input">

    <input type="hidden" name="releaseTime" th:value="${clazz.releaseTime}" class="layui-input">

    <input type="hidden" name="status" th:value="${clazz.status}" class="layui-input">
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
</div>
<script th:src="@{/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form','laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        // 二级联动 第一步 渲染父级
        $.ajax({
            url: '/college/lists',	// 后台取到所有的科室名称
            dataType: 'json',
            type: 'get',
            success: function (resData) {
                if ( resData.code == 0 ){
                    $.each(resData.data, function (index, value) {
                        // 前者是页面显示的值，后者是传递给后台的值。
                        // 正常情况下，后者应该是 value.id
                        $('#collegeName').append(new Option(value.collegeName, value.collegeName));	// 下拉菜单里添加元素
                    });
                    //渲染select
                    form.render('select');
                }
            }
        });



        // 第二步 监听父级，每当父级发生变化时，渲染子级的值
        form.on('select(collegeName)', function(data){
            var Name = data.value; // roomName 选中的科室名称
            $.ajax({
                url: '/major/search',
                dataType: 'json',
                type: 'get',
                data: {collegeName:Name},		// 传入科室名称，查找该科室的所有床号
                success: function (resData) {
                    if ( resData.code == 0 ){
                        // 清空 之前的 专业
                        $('#majorName').empty();
                        $.each(resData.data, function (index, value) {
                            // 这里的 value.bedNo
                            // 前者是页面显示的值，后者是传递给后台的值。
                            // 正常情况下，后者应该是 value.id
                            $('#majorName').append(new Option(value.majorName, value.majorName));// 下拉菜单里添加元素
                        });
                        //渲染 select
                        form.render('select');
                    }
                }
            });
        });


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            console.log(data)
            $.ajax({
                url: '/clazz/put',
                method: 'POST',
                data: data.field,
                success:function (res) {
                    if (res.code == 0 ){
                        layer.msg("保存成功")
                        setTimeout(function () {
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        },1500)
                    }else {
                        layer.msg("保存失败")
                    }
                }
            })
            return false;
        });

    });
</script>
</body>
</html>